package geppetto.cat.programs;


import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.cat.alignments.output.AlignerOutput;
import geppetto.cat.corpus.BilingualCorpus;

import java.io.IOException;
import java.io.PrintStream;



public class CompareGoldAlignmentSets {

	/**
	 * Compares the alignments between two annotators results is as follow: SS
	 * SP PS PP S0 0S P0 0P total A1 total A2 Equal Sentences Total Sentences
	 */
	public static int[] compareAlignmnet(Alignment al1, Alignment al2) {
		int[] result = new int[13];
		boolean equalSentence = true;
		boolean softEqualSentence = true;
		for (int foreignIndex = 0; foreignIndex < al1.getForeignLenght(); foreignIndex++) {
			for (int sourceIndex = 0; sourceIndex < al1.getSourceLenght(); sourceIndex++) {
				if (al1.isSure(sourceIndex, foreignIndex)) {
					if (al2.isSure(sourceIndex, foreignIndex)) {
						// both sure
						result[0]++;
					} else if (al2.isPossible(sourceIndex, foreignIndex)) {
						// a1 sure a2 possible
						result[1]++;
						equalSentence = false;
					} else {
						// a1 sure a2 null
						result[4]++;
						equalSentence = false;
						softEqualSentence = false;
					}
				} else if (al1.isPossible(sourceIndex, foreignIndex)) {
					if (al2.isSure(sourceIndex, foreignIndex)) {
						// a1 possible a2 sure
						result[2]++;
						equalSentence = false;
					} else if (al2.isPossible(sourceIndex, foreignIndex)) {
						// a1 possible a2 possible
						result[3]++;
					} else {
						// a1 possible a2 null
						equalSentence = false;
						softEqualSentence = false;
						result[6]++;
					}
				} else {
					if (al2.isSure(sourceIndex, foreignIndex)) {
						// a1 empty a2 sure
						result[5]++;
						equalSentence = false;
						softEqualSentence = false;
					} else if (al2.isPossible(sourceIndex, foreignIndex)) {
						// a1 null a2 possible
						softEqualSentence = false;
						equalSentence = false;
						result[7]++;
					}
				}
			}
		}
		result[8] += al1.numberOfAlignedPoints();
		result[9] += al2.numberOfAlignedPoints();
		if (equalSentence)
			result[10]++;
		if (softEqualSentence)
			result[11]++;
		result[12]++;
		return result;
	}

	public static void printResult2(int[] result) {

		double a1, a2, sa, wa, wd, sd;
		a1 = result[8];
		a2 = result[9];
		sa = 100.0 * 2 * (result[3] + result[0]) / (a1 + a2);
		wa = 100.0 * 2 * (result[1] + result[2]) / (a1 + a2);
		wd = 100.0 * (result[6] + result[7]) / (a1 + a2);
		sd = 100.0 * (result[4] + result[5]) / (a1 + a2);
		System.out.println("Size of A1: " + a1);
		System.out.println("Size of A2: " + a2);

		System.out.println("Strong Agreement: " + sa);
		System.out.println("Weak Agreement: " + wa);
		System.out.println("Weak DisAgreement: " + wd);
		System.out.println("Strong DisAgreement: " + sd);
		System.out.println("Correct Sentences: "
				+ (1.0 * result[10] / result[12]));
		System.out.println("Soft Correct Sentences: "
				+ (1.0 * result[11] / result[12]));
		System.out.println("Total: " + (sa + wa + wd + sd));
	}

	public static void printResult(int[] result) {
		System.out.println("Sure -- Sure: " + result[0]);
		System.out.println("Sure -- Possible: " + result[1]);
		System.out.println("Possible -- Sure: " + result[2]);
		System.out.println("Possible -- Possible: " + result[3]);
		System.out.println("Sure -- Empty: " + result[4]);
		System.out.println("Empty -- Sure: " + result[5]);
		System.out.println("Possible -- Empty: " + result[6]);
		System.out.println("Empty -- Possible: " + result[7]);
	}

	public static void printReport(PrintStream out, AlignmentsSet a1,
			AlignmentsSet a2, String author1, String author2,
			BilingualCorpus corpus) {
		int[] total = new int[13];
		System.out.println();
		System.out.println();
		for (int i = 0; i < a1._alignments.size(); i++) {
			System.out.println(author1);
			AlignerOutput.output(a1.get(i), corpus, System.out);
			System.out.println(author2);
			AlignerOutput.output(a2.get(i), corpus, System.out);
			System.out.println("Statistics");
			int[] result = compareAlignmnet(a1.get(i), a2.get(i));
			printResult(result);
			for (int j = 0; j < result.length; j++) {
				total[j] += result[j];
			}
			System.out.println();
			System.out.println();
		}
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("Global Statistics");
		System.out.println(author1 + "   " + author2);
		printResult(total);
		System.out.println();
		System.out.println();
		printResult2(total);
	}

	public static void main(String[] args) throws IOException {
		String corpusFile = args[0];
		String al1File = args[1];
		String al2File = args[2];
		int initialSentence = Integer.parseInt(args[3]);
		String author1 = args[4];
		String author2 = args[5];
		BilingualCorpus corpus = BilingualCorpus.getCorpusFromFileDescription(
				corpusFile, Integer.MAX_VALUE);
		AlignmentsSet a1 = AlignmentsSet.getAlignmentFromFileNAACL(al1File,
				(byte) 0, corpus, initialSentence);
		AlignmentsSet a2 = AlignmentsSet.getAlignmentFromFileNAACL(al2File,
				(byte) 0, corpus, initialSentence);
		printReport(System.out, a1, a2, author1, author2, corpus);

	}
}
